package cn.fengwoo.springBoot.handle;

import cn.fengwoo.springBoot.service.TokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by fangdaji on 2018/1/11.
 */
@Component
public class AccessTokenVerifyInterceptor implements HandlerInterceptor {

    private final static Logger logger = LoggerFactory.getLogger(AccessTokenVerifyInterceptor.class);

    @Autowired
    private TokenService tokenService;


    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        //response.setContentType("application/json; charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");

        String requestToken = request.getHeader("access-token");
        Boolean check = tokenService.checkAccessToken(requestToken);

        return check;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }


}
